<?php
session_start();
class Common
{
    public static function prepareGPCData( & $var )
    {
		if( is_array( $var ) )
		{
			while( list( $key , $val ) = each( $var ) )
			{
				$var[$key] = self::prepareGPCData( $val );
			}
		}
		else
		{
			$var = Helper_String::encode( $var );
		}

		return $var;
    }
    
    /**
     * 获取系统配置
     * @return mixed
     */
    
    private static function _getSysConfig( $key = false )
    {
        if( file_exists( CONFIG_DIR . "/SystemConfig.php" ) )
        {
            $config = require( CONFIG_DIR . "/SystemConfig.php" );
        }
       if( $key !== false )
        {
          return $key;
        }
        
        return $config;
    }

    /**
	 * @brief	获取系统配置信息
	 * @param	$key	配置文件项
	 * @return 	Array
	 */
	public static function & getConfig( $key = false )
	{
               // echo $key;
		static $config = array();
		if( empty( $config ) )
		{
                    
                    $config = array();
                    
                    if( !self::isDev() )
                    {
                        
                        if( file_exists( CONFIG_DIR . "/SystemConfig.php" ) )
                        {
                            $config = require( CONFIG_DIR . "/SystemConfig.php" );
                        }
                    }
                    else
                    {
                       
                        if( file_exists( CONFIG_DIR . "/LocalSystemConfig.php" ) )
                        {
                            $config = require( CONFIG_DIR . "/LocalSystemConfig.php" );
                        }
                     }
		}
		if( ( $key !== false ) && !isset( $config[ $key ] ) )
		{
			if( file_exists( CONFIG_DIR . "/{$key}.php" ) )
			{
				$config[ $key ] = require( CONFIG_DIR . "/{$key}.php" );
			}
		}

		if( $key !== false )
		{     
		   return $config[ $key ];
		}
		return $config;
	}

	/**
	 * 获取语言包
	 *
	 * @param string $package
	 */
	public static function & getLang( $package )
	{
	    static $langConfig = array();
	    if( empty( $langConfig[$package] ) )
	    {
	        if( file_exists( LANG_DIR . "Common.php" ) )
			{
				$langConfig[$package] = require( LANG_DIR . "Common.php" );
			}
	        if( file_exists( LANG_DIR . "{$package}.php" ) )
			{
				$langConfig[$package] = array_merge( $langConfig[$package] , require( LANG_DIR . "{$package}.php" ) );
			}
	    }

	    return $langConfig[$package];
	}

	/**
	 * 获取Cache实例
	 * @param	string $param	Cache服务器名称
	 * @return	Core_Cache_Memcached
	 */
	public static function & getCache( $param = 'data' )
	{
		static $cache = array();
		if( empty( $cache[$param] ) )
		{
			$config = self::getConfig( 'memcache' );
			if( empty( $config[$param] ) )
			{
				throw new Core_Exception( Core_Exception::SYSTEM_CACHE_NO_CONFIG );
			}
			else
			{
				$memcacheClass = 'Helper_Cache_' . self::getConfig( 'memcacheClass' );
				$cache[$param] = new $memcacheClass( $config[$param] );
			}
		}
		
		return $cache[$param];
	}

	/**
	 * 根据数据库名获取数据库
	 *
	 * @param string $dbName
	 * @return Helper_MysqlDb
	 */
	public static function getMysql( $dbName = 'common' )
	{
                //echo $dbName;
		static $mysqlObject = array();
		if( empty( $mysqlObject[ $dbName ] ) )
		{    
                      //获取数据库配置信息
			$mysqlConfig = & Common::getConfig( 'mysqlDb' );
                        // print_r($mysqlConfig) ;
                        //echo $dbName;
			if( empty( $mysqlConfig[ $dbName ] ) )
                        {
                            throw new AppException( 4 );
                        }
                       
                        // print_r( $mysqlConfig[ $dbName ]);
			$mysqlObject[ $dbName ] = new Helper_MysqlDb( $mysqlConfig[ $dbName ] );
		}
            
		//print_r($mysqlObject[ $dbName ]);
                
		return $mysqlObject[ $dbName ];
	}
    
    /**
     * 当前是否为开发环境
     * @return boolean
     */
    public static function isDev()
    {
        return self::_getSysConfig( 'dev' );
    }
}
/**
 * 自动加载类文件
 *
 * @param string $className
 */
function __autoload(  $className )
{
       // echo $className;
	//切分类的目录
	if( strpos( $className , "_" ) !== false )
	{
		$className = str_replace( "_" , "/" , $className );
	}

	//在模块文件夹搜索
	if( file_exists( MOD_DIR . "/{$className}.php" ) )
	{
		include_once( MOD_DIR . "/{$className}.php" );
		return ;
	}

	//在核心文件夹搜索
	if( file_exists( CORE_DIR . "/{$className}.php" ) )
	{
		include_once( CORE_DIR . "/{$className}.php" );
		return ;
	}

    //在控制器文件夹搜索
	if( file_exists( CON_DIR . "/{$className}.php" ) )
	{
		include_once( CON_DIR . "/{$className}.php" );
		return ;
	}

	//在第三方文件夹搜索
	if( file_exists( THIRD_DIR . "/{$className}.php" ) )
	{
		include_once( THIRD_DIR . "/{$className}.php" );
		return ;
	}
}